NetWorthCalculator

NetWorthCalculator objects will calculate the net worth of the specified data file. The calculation can observe or ignore account-specific flags for whether they should be included in net worth calculations. All methods that return a TotalAmount instance, return a duplicated copy of TotalAmount. NetWorthCalculator's internal TotalAmount instance cannot be directly changed by the caller. (Except netWorthBalances: its getBalance() / getBalances() return the stored TotalAmount objects - do not change them.)

By default: current balance (as-of today) will be used; account specific net worth exclusion flags will be applied; children of excluded parents will also be excluded.

The properties asOfDate or asOfDates should normally be null (default) for current or future balances. Only set one of these when you want to obtain the networth as-of a specific date(s) (in which case, balance type will be ignored). NOTE: Using as-of date(s) is a slower calculation as this has to scan transactions.

NOTES:

  • Care should be taken when converting any net worth grand total between currencies to avoid rounding methodology differences. The general principle applied by Moneydance, is to total transactions by account, in that account's own currency. Then convert from that account's currency to the final target currency in one step, and then add that account's converted balance into the net worth total. Generally speaking, you should not just convert a net worth total between currencies, nor should you convert account totals using intermediate currencies.

  • Account opening balances will always be included if that account's creation date -OR- any date qualifying transactions are found. (i.e. if the account's creation date is after the as-of date, but transactions on/before the as-of date are found, then the opening balance is included). An account with no (valid) creation date and no transactions on/before the as-of date gets no opening balance (today's balance still includes it). This consideration not relevant when using asOfDate set to null for today's current/future balance.

Performance considerations:

  • when using asOfDate / asOfDates set to null, then today's date is processed (in conjunction with balance type). This method does not scan transactions and simply processes each account once to retrieve it's balance. Performance: O(A), where A = number of accounts - very fast and suitable for near real-time use.

  • all as-of date scenarios below first scan all of the book's transactions once to select the accounts' transactions (T below).

  • when using a single as-of date, no transaction sorting takes place, each transaction is processed once, and each account is visited two-three times. Performance: O(T + 2A), where T = transactions, A = accounts - highly efficient for one-off calculations.

  • when using multiple as-of dates, transactions are sorted, and each transaction is processed once. the code is fast for accounts with no splits, and slightly slower for (security) accounts with (stock) splits....

  • for security accounts with stock splits, txn's value(s) are adjusted for splits and accumulated into the current and all future periods (per txn).

  • for accounts with no splits, txn value(s) are accumulated per account, per as-of date period, and then these balances are accumulated once into the current and all future periods. Performance >> Non-split accounts: O(T + D × A). Security accounts with stock splits: O(T × D′). Where: T = txn, D = date, A = account, D′ ≈ number of as-of dates ≥ txn date (plus the O(T log T) date sort) Efficient for large-scale usage (e.g. 200k txns × 200 accts × 7300 dates)

  • any optional call to preCalculateMinMaxRecordDates, has similar characteristics to the single as-of date scenario, but no balances are calculated Performance: O(T + A), where T = transactions, A = accounts - highly efficient as no balances are calculated (one zero balance object per account).

Since

Moneydance 2023.2 build 5007. Updated MD2024.3(5202) to not check account active/inactive status. Updated MD2027 with enhanced / faster as-of date capabilities, and to provide results to all consumers of net worth values (i.e. net worth graph, report, view, dashboard)

Constructors

Link copied to clipboard
constructor(book: AccountBook, currency: CurrencyType)
constructor(book: AccountBook)

Constructs a NetWorthCalculator using the currency of the supplied book

Types

Link copied to clipboard
data class AccountBalancesByAsOfDates(val accounts: Set<Account>, val asOfDates: IntArray? = null, val balanceType: Account.BalanceType? = null, val outOfDateNull: Boolean = false)

Data storage class to hold account balances (either by as-of dates or for current/future balances) (within TotalAmount objects). When supplied, the order of the list of asOfDates is always preserved.

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

AccountFilter can be used to determine the accounts that are included in the net worth calculation. By default it includes net worth eligible accounts flagged for inclusion in net worth (and not under an excluded parent), then applies additionalAccountFilter

Link copied to clipboard

This optional filter will be applied as a secondary account filter after the primary net worth filter accountFilter has filtered accounts for inclusion/exclusion. Once an account for inclusion is identified, this secondary filter allows the caller to create a subset of net worth. For example, a report might want a net worth balance of just two accounts. NOTE: this filter will not allow accounts excluded by the primary filter to be included!

Link copied to clipboard

Retrieve the single as-of date (or null (default) for today) used with this calculator instance. Do not use when multiple as-of dates are being calculated! Setting an invalid date (or today) is treated as null (today); an invalid date in asOfDates throws when calculated.

Link copied to clipboard

Optional: Specify multiple as-of dates are to be calculated - null (default) signifies a single as-of date (today) is being calculated.

Link copied to clipboard

Specify the balance type to be calculated (normally Account.BalanceType.CURRENT (default for current balance) or Account.BalanceType.NORMAL (for future balance)) when calculating a single net worth value for today (by setting asOfDate to null).

Link copied to clipboard

Changes the stored currency for this NetWorthCalculator instance (this does nothing if called with the existing currency). If a net worth calculation has already been run, then the grand total will be converted to the new currency. This conversion does not re-run the whole calculation, but it will re-calculate the grand total by converting all account / date balances to the new currency

Link copied to clipboard

Optional: Specify a cut off date for records. using this ensures that no data newer than the specified date is included. This is typically (only) used when graphing data over time and the generator passes intervals (list of as-of dates) to the calculator that go beyond the actual end date (e.g. when group by WEEK/MONTH/YEAR etc). This ensures no data beyond this date is included in as-date balances (that may extend past this date)

Link copied to clipboard

Specifies whether children of excluded accounts are also excluded - normally true

Link copied to clipboard

Signifies whether the net worth calculation has been executed

Link copied to clipboard

When true then all accounts that are normally eligible for net worth will be included (i.e. it overrides any accounts with the exclude from net worth flag set)

Link copied to clipboard

Stores the results of the net worth calculations (by account / as-of date). Can convert balances / totals between currencies

Functions

Link copied to clipboard
fun calculateTotal(highlightInLog: Boolean = false): TotalAmount

Executes the NetWorth calculation using the pre-specified properties. Returns a copy of the calculated TotalAmount

Link copied to clipboard

Retrieves the latest net worth result (as a duplicated TotalAmount object)- the caller should confirm that the calculation has been run before calling

Link copied to clipboard

Pre-Calculate the minimum and maximum record dates from transaction and account start dates that can be used for a networth calculation. Will return null when there are not enough records to deduce the range. Typically used to pre-determine date bounds before running a full net worth balance calculation (e.g. for chart scaling, filtering, etc.). You should only call this once, and only when needed at the beginning of your code, as it does have to quickly scan all transactions.

Link copied to clipboard
open override fun toString(): String